﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using AppLayer;
using SpotItServerAPI;
using SpotItComponents;
using System.Drawing;
using System.Drawing.Imaging;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_Deck_Editor.xaml
    /// </summary>
    public partial class SpotIt_Deck_Editor : Window
    {
        private int imageCount = 0;
        public SymbolSet CurrentSymbolSet { get; set; }
        private List<Bitmap> bitmapList = new List<Bitmap>();
        private int blank;
        private List<System.Windows.Controls.Image> imageList = new List<System.Windows.Controls.Image>();
        Session session;
        Services services;
        public SpotIt_Deck_Editor(Session sessions, Services service)
        {
            InitializeComponent();
            this.session = sessions;
            this.services = service;
            imageBox.ItemsSource = imageList;            
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            String[] txtBrowse;
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.Title = "Select an image file";
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    txtBrowse = dialog.FileNames;
                    foreach (String i in txtBrowse)
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(i, UriKind.Absolute);
                        bi.DecodePixelWidth = 150;
                        bi.DecodePixelHeight = 150;
                        bi.EndInit();
                        image.Source = bi;
                        MemoryStream TransportStream = new MemoryStream();
                        BitmapEncoder enc = new BmpBitmapEncoder();
                        enc.Frames.Add(BitmapFrame.Create(bi));
                        enc.Save(TransportStream);
                        Bitmap bit = new Bitmap(TransportStream);
                        bitmapList.Add(bit);
                    }
                    draw();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Error opening file " + ex.Message.ToString(), "File Error",
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                }
            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            SymbolSetPopulator populator = new SymbolSetPopulator(session, services);
            populator.populateSet(CurrentSymbolSet, bitmapList);
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            imageCount = CurrentSymbolSet.Symbols.Count;
            for (int i = 0; i < imageCount; i++)
            {
                MemoryStream ms = new MemoryStream();
                CurrentSymbolSet.Symbols[i].SymbolImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(ms.ToArray());
                bi.EndInit();
                MemoryStream TransportStream = new MemoryStream();
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bi));
                enc.Save(TransportStream);
                Bitmap bit = new Bitmap(TransportStream);
                bitmapList.Add(bit);
            }
            draw();
        }

        private void changeButton_Click(object sender, RoutedEventArgs e)
        {
            int ind = imageBox.SelectedIndex;
            if (ind == -1)
                System.Windows.Forms.MessageBox.Show("No Symbol Selected");
            else
            {
                String txtBrowse;
                System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
                dialog.Filter =
                   "All files (*.*)|*.*";
                dialog.Title = "Select an image file";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        txtBrowse = dialog.FileName;
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(txtBrowse, UriKind.Absolute);
                        bi.DecodePixelWidth = 150;
                        bi.DecodePixelHeight = 150;
                        bi.EndInit();
                        MemoryStream TransportStream = new MemoryStream();
                        BitmapEncoder enc = new BmpBitmapEncoder();
                        enc.Frames.Add(BitmapFrame.Create(bi));
                        enc.Save(TransportStream);
                        Bitmap bit = new Bitmap(TransportStream);
                        bitmapList[ind] = bit;
                        draw();
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("Error opening file " + ex.Message.ToString(), "File Error",
                        System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                    }
                }
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            int ind = imageBox.SelectedIndex;
            imageCount--;
            if (ind == -1)
                System.Windows.Forms.MessageBox.Show("No Symbol Selected");
            else
            {
                bitmapList.RemoveAt(ind);
                draw();
            }
        }

        private void draw()
        {
            imageList.Clear();
            foreach (Bitmap i in bitmapList)
            {
                System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                MemoryStream TransportStream = new MemoryStream();
                i.Save(TransportStream, ImageFormat.Png);
                TransportStream.Position = 0;
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = TransportStream;
                bi.DecodePixelWidth = 150;
                bi.DecodePixelHeight = 150;
                bi.EndInit();
                image.Source = bi;
                imageList.Add(image);
                imageBox.Items.Refresh();
                countLbl.Content = "This Symbol Set contains " + imageCount + " images.";
            }
        }
    }
}
